import axios from 'axios';
import { Message ,Loading } from 'element-ui';

const axiosserver = axios.create({
    baseURL:'http://127.0.0.1:5000/v1',
    timeout: 2000,
})

let loading
function startLoading(){
    loading = Loading.service({
        lock:true,
        text:'拼命加载中……',
        background:'rgba(0,0,0,.3)'
    });
}

function endloading(){
    loading.close()
}
// 请求拦截
axiosserver.interceptors.request.use(function(config){
    // 加载动画
    startLoading()

    if(localStorage.eleToken){
        // 设置统一的请求头
        config.headers.Authorization = localStorage.eleToken
    }

    return config
},error=>{
    return Promise.reject(error)
})

// 响应拦截
axiosserver.interceptors.response.use(function(response){
    // 结束加载动画
    endloading()
    return response
},error=>{
    // 错误提醒
    endloading()
    Message.error(error.response)
    return Promise.reject(error)
})





export default axiosserver;